package org.company.management.ui;

import org.company.management.controller.UiController;
import org.company.management.utilities.Strings;
import org.eclipse.nebula.widgets.pshelf.PShelf;
import org.eclipse.nebula.widgets.pshelf.PShelfItem;
import org.eclipse.nebula.widgets.pshelf.RedmondShelfRenderer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.SWTResourceManager;

/**
 * @author Paner
 *
 */
public class MenuView extends ViewPart implements SelectionListener {
	
	public final static String ID = Strings.menuViewId;
	
	private PShelf _pShelf;
	private PShelfItem _homeItem;
	private PShelfItem _employeesPshef;
	private TableItem  _galleryShowItem;
	private TableItem _tableShowItem;
	private PShelfItem _scheduleItem;
	private TableItem  _newScheduleItem;
	private TableItem _editScheduleItem;
	private PShelfItem _groupItem;
	

	public MenuView() {
		// TODO Auto-generated constructor stub
		
	}

	
	@Override
	public void createPartControl(Composite parent) {
		createControls(parent);
		
	}

	/*
	 * Creates widgets
	 */
	private void createControls(Composite parent) 
	{
		_pShelf = new PShelf(parent, SWT.NONE);
		_pShelf.setRenderer(new RedmondShelfRenderer());
		_pShelf.addSelectionListener(this);
		
		_homeItem = new PShelfItem(_pShelf, SWT.NONE);
		_homeItem.setText(Strings.home);
		
		_employeesPshef = new PShelfItem(_pShelf, SWT.NONE);
		_employeesPshef.getBody().setBackground(SWTResourceManager.getColor(230, 230, 250));
		_employeesPshef.setText(Strings.employees);
		
		_scheduleItem = new PShelfItem(_pShelf, SWT.NONE);
		_scheduleItem.setText(Strings.schedule);
		_scheduleItem.getBody().setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Table table = new Table(_scheduleItem.getBody(),SWT.NONE);
	    table.addSelectionListener(this);
	    
	    _newScheduleItem = new TableItem(table,SWT.NONE);
	    _newScheduleItem.setText(Strings.gallery);
	    
	    _editScheduleItem = new TableItem(table,SWT.NONE);
	    _editScheduleItem.setText(Strings.ShowTable);
		
		_groupItem = new PShelfItem(_pShelf, SWT.NONE);
		_groupItem.setText(Strings.group);
	    _employeesPshef.getBody().setLayout(new FillLayout(SWT.HORIZONTAL));
	    
	    table = new Table(_employeesPshef.getBody(),SWT.NONE);
	    table.addSelectionListener(this);
	    
	    _galleryShowItem = new TableItem(table,SWT.NONE);
	    _galleryShowItem.setText(Strings.gallery);
	    
	    _tableShowItem = new TableItem(table,SWT.NONE);
	    _tableShowItem.setText(Strings.ShowTable);
		
	}

	@Override
	public void widgetSelected(SelectionEvent e)
	{
		Object selectedItem = null;
		try
		{
		selectedItem = ((PShelf) e.getSource()).getSelection();
		}
		catch(Exception ex)
		{
			selectedItem = ((Table) e.getSource()).getSelection()[0];
		}		

		if(selectedItem == _homeItem)
		{
			UiController.showEditor(Strings.home, MainEditor.ID);
		}
		else if (selectedItem == _employeesPshef || selectedItem == _galleryShowItem)
		{
			UiController.showEditor(Strings.gallery, EmployeeGalleryEditor.ID);
		}
		else if (selectedItem == _tableShowItem)
		{
			UiController.showEditor(Strings.ShowTable, EmployeeTableEditor.ID);
		}
		else if (selectedItem == _scheduleItem || selectedItem == _newScheduleItem)
		{
			UiController.showEditor(Strings.schedule, ScheduleEditor.ID);
		}
		else if (selectedItem == _editScheduleItem)
		{
			UiController.showEditor(Strings.schedule, EditScheduleEditor.ID);
		}
		else if (selectedItem == _groupItem)
		{
			UiController.showEditor(Strings.groups, GroupEditor.ID);
		}
		
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
}
